<template>
  <view>
    <!-- 一行两列楼盘展示 -->
    <view class="buildings-list" v-if="type == 'twoColumns'">
      <view v-for="(item, index) in res" :key="index" class="building-item">
        <view class="image-wrapper" @click="navigateToDetailPage(item)">
          <u-image :src="parseImage(item.images)" width="100%" height='330rpx' mode="aspectFit">
            <u-loading slot="loading"></u-loading>
          </u-image>
        </view>
        <view class="building-detail">
          <div class="title clamp" @click="navigateToDetailPage(item)">
            {{ item.buildingName }}
          </div>
          <view class="price-box" @click="navigateToDetailPage(item)">
            <div class="price">
              ¥<span>{{ item.price }}</span>
            </div>
          </view>
          <div class="desc" @click="navigateToDetailPage(item)">
            {{ item.address }} || {{ item.layout }}
          </div>
          <div class="store-seller-name" v-if="storeName" @click="navigateToStoreDetailPage(item)">
            <div class="text-hidden">
              <u-tag style="margin-right: 10rpx" size="mini" mode="dark" v-if="item.selfOperated"
                     text="自营" type="error" />
              <span>{{ item.developerName || "暂无" }}</span>
            </div>
            <span>
                            <u-icon name="arrow-right"></u-icon>
                        </span>
          </div>
        </view>
      </view>
    </view>
    <!-- 一行一列楼盘展示 -->
    <div v-if="type == 'oneColumns'"  class="buildings-one-row">
      <div v-for="(item, index) in res" :key="index" class="building-row">
        <div class="flex building-col">
          <div class="building-img" @click="navigateToDetailPage(item)">
            <u-image width="230rpx" mode="aspectFit" border-radius='16' height="230rpx" :src="parseImage(item.images)">
              <u-loading slot="loading"></u-loading>
            </u-image>
          </div>
          <div class="building-detail">
            <div class="title clamp3" @click="navigateToDetailPage(item)">{{ item.buildingName }}</div>
            <view class="price-box" @click="navigateToDetailPage(item)">
              <div class="price">
                ¥<span>{{ item.price }}</span>
              </div>
            </view>
            <div class="desc" @click="navigateToDetailPage(item)">
              {{ item.address }} || {{ item.layout }}
            </div>
            <div style="overflow: hidden" @click="navigateToStoreDetailPage(item)" class="count-config">
              <div class="text-hidden" v-if="storeName">
                <u-tag style="margin-right: 10rpx" size="mini" mode="dark" v-if="item.selfOperated"
                       text="自营" type="error" />
                <span class="line1-store-name">{{ item.developerName }}</span>
                <span class="to-store">进店<u-icon size="24" name="arrow-right" color="#666"></u-icon>
                                </span>
              </div>
              <span>
                                <u-icon name="arrow-right" color="#c5c5c5"></u-icon>
                            </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </view>
</template>

<script>
import commonTpl from '@/components/m-goods-list/common'
export default {
  data() {
    return {
      lightColor: this.$mainColor
    }
  },
  mixins: [commonTpl],
  props: {
    // 遍历的数据
    res: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 一行两列还是一行一列显示
    type: {
      type: String,
      default: 'twoColumns',
      validator() {
        return ['twoColumns', 'oneColumns']
      }
    },
    storeName: {
      type: Boolean,
      default: true
    },
    keyword: {
      type: null,
      default: ''
    }
  },
  watch: {
    keyword(val) {
      if (val) {
        this.lightSearchStr(val)
      }
    }
  },
  methods: {
    parseImage(images) {
      // 这里可根据实际情况处理图片，示例直接返回
      return  JSON.parse(images)[0];
    },
    // 跳转到楼盘详情
    navigateToDetailPage(item) {
      this.$u.route({
        url: '/pages/store/buildingDetail',
        params: {
          id: item.id
        }
      })
      // uni.navigateTo({
      //   url: `/pages/building/buildingInfo?id=${item.id}&buildingId=${item.buildingId}`,
      // });
    },
    // 跳转地址
    navigateToStoreDetailPage(item) {
      uni.navigateTo({
        url: `/pages/building/developerPage?id=${item.developerId}`,
      });
    },
  }
}
</script>

<style lang='scss' scoped>
.buildings-one-row{
  padding-bottom: 250rpx;
}
/* 楼盘列表 */
.buildings-list {
  display: flex;
  flex-wrap: wrap;
  margin: 10rpx 20rpx 284rpx;
  width: 100%;

  >.building-item {
    background-color: #ffffff;
    display: flex;
    border-radius: 16rpx;
    flex-direction: column;
    width: calc(50% - 30rpx);
    margin-bottom: 20rpx;
    padding-bottom: 20rpx;

    &:nth-child(2n + 1) {
      margin-right: 20rpx;
    }

    .image-wrapper {
      width: 100%;
      height: 330rpx;
      border-radius: 16rpx 16rpx 0 0;
      overflow: hidden;
      padding: 0;
    }
  }

  .count-config,
  .store-seller-name {
    font-size: $font-sm;
  }

  .text-hidden {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.building-row {
  background: #fff;
  padding: 16rpx;

  >.building-col {
    display: flex;

    >.building-img {
      overflow: hidden;
      flex: 4;
    }

    >.building-detail {
      flex: 7;
    }
  }
}

.building-detail {
  margin: 0 20rpx;

  >.title {
    font-size: $font-base;
    color: $font-color-dark;
    line-height: 1.5;
    height: 42rpx;
    padding: 10rpx 0 0;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }

  .desc {
    margin-top: 4rpx;
    font-size: 28rpx;
    color: #666;
  }

  .store-seller-name {
    color: #666;
    overflow: hidden;
    display: flex;
    justify-content: space-between;
  }

  .count-config {
    padding: 5rpx 0;
    color: #666;
    display: flex;
    font-size: 28rpx;
    justify-content: space-between;
  }

  >.price-box {
    margin-top: 10rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-right: 10rpx;
    font-size: 28rpx;
    color: $font-color-light;

    >.price {
      font-size: 26rpx;
      line-height: 1;
      color: $main-color;
      font-weight: bold;

      ::v-deep span:nth-of-type(1) {
        font-size: 38rpx;
      }
    }
  }
}
</style>